// 在真实环境中， 如果使用firebase这种第三方auth服务的话，本文件不需要开发者开发

import { User } from "./screens/project-list/search-panel";

const localStoreageKey = "__auth_provider_token__";
const apiUrl = process.env.REACT_APP_API_URL;

export const getToken = () => window.localStorage.getItem(localStoreageKey);

export const handleUserResponse = ({ user }: { user: User }) => {
    window.localStorage.setItem(localStoreageKey, user.token || "");
    return user;
};

export const login = (data: { username: string; password: string }) => {
    return fetch(`${apiUrl}/login`, {
        method: "POST",
        headers: {
            "Content-type": "application/json",
        },
        body: JSON.stringify(data),
    }).then(async (res: Response) => {
        console.log(res);
        if (res.ok) {
            return handleUserResponse(await res.json());
        } else {
            return Promise.reject(data);
        }
    });
};

export const register = (data: { username: string; password: string }) => {
    return fetch(`${apiUrl}/register`, {
        method: "POST",
        headers: {
            "Content-type": "application/json",
        },
        body: JSON.stringify(data),
    }).then(async (res: Response) => {
        console.log(res);
        return handleUserResponse(await res.json());
    });
};

export const logout = async () =>
    window.localStorage.removeItem(localStoreageKey);
